angular.module('videomorecontrollers', [])
//更多视频的Controller
    .controller('VideoMoreCtrl',function($scope,$state,$rootScope,$ionicHistory,$timeout,$ionicSlideBoxDelegate,$ionicScrollDelegate,Toast,Loading,ENV,Video,History,Magazine,Storage,Member){
        var memberId ='';
        $scope.videoList = [];

        $scope.picPath = ENV.picPath;
        Loading.showLoading();
        Video.getVideoList(memberId,7);//加载视频列表
        $scope.showWhich = [true,false,false];

        //== 暂时id start ==
        $scope.result = {
            member_id : 626
        };
        //返回
        $scope.goback = function(){
            History.goback();
        };
        //== 暂时id begin ==

        //进入页面的时候调用
        $scope.$on('$ionicView.beforeEnter',function(){
            $rootScope.showWineGlasses = false;
            var member = Storage.get('member');
            if(!member || !member.member_id){
                Storage.set('member', $scope.result);
                memberId =  $scope.result.member_id;
            }else{
                memberId =  member.member_id;
            }
            searchList = [];
            for(i=0;i<$scope.videoList.length;i++) {
                search = {
                    mc_obj_id: $scope.videoList[i].video_id,
                    member_id: memberId,
                    mc_obj_type:5
                }
                searchList.push(search);
            }
            if(searchList.length>0){
                Video.getCollectInfo('video',searchList);
            }
        });
        $rootScope.goToWineIndex = function(){
            $rootScope.showWineGlasses = true;
            $timeout(function(){
                $state.go('productIndex');
            },100);
        }
        $scope.$on('BookShelf.getCollectInfoUpdated',function(){
            var result = Video.getCollectInfoJson();
            var list = result.collectInfo;
            if(result.type=='video'){//视频
                for(i=0;i<$scope.videoList.length;i++){
                    for(j=0;j<list.length;j++){
                        if($scope.videoList[i].video_id==list[j].id){
                            $scope.videoList[i].vcStatus = list[j].state;
                        }
                    }
                }
            }
        });
        //下拉刷新
        $scope.doRefreshVideo = function(){Video.getVideoList(memberId);}

        $scope.loadMoreVideo = function(){
            Video.getMoreVideoList(memberId);
        }

        //视频是否有下一页
        $scope.videoHasNextPage = function(){
            return Video.videoHasNextPage();

        }
        $scope.$on('Video.MoreVideoListUpdated',function(){
            $scope.videoList = Video.getVideoListJson();
            $scope.$broadcast('scroll.infiniteScrollComplete');
        });

        /*******显示视频*******/
        $scope.showVideo = function(){
            showVideoFun();
        };
        showVideoFun = function(){
            $ionicScrollDelegate.scrollTop();
            $scope.showWhich = [true,false,false];
            if($scope.videoList!=undefined&&$scope.videoList.length>0){
                searchList = [];
                for(i=0;i<$scope.videoList.length;i++) {
                    search = {
                        mc_obj_id: $scope.videoList[i].video_id,
                        member_id: memberId,
                        mc_obj_type:5
                    }
                    searchList.push(search);
                }
                if(searchList.length>0){
                    Video.getCollectInfo('video',searchList);
                }
            }
        }
        //获取网络数据成功的后调用该方法
        $scope.$on('Video.videoListUpdated', function(){
            $scope.$broadcast('scroll.refreshComplete');
            $scope.videoList = Video.getVideoListJson();
            Loading.hideLoading();
        });
        /*******显示视频结束*******/

        //收藏视频
        var collectIndex = '';
        var collectVideoing = false;
        $scope.collectVideo = function(videoId,index){

            var member = Storage.get('member');
            if(!member || !member.member_id || member.member_id == 626){
                Member.isLogOut(false);
                $state.transitionTo('login');
            }else{
                if(!collectVideoing){
                    collectVideoing = true;
                    Loading.showLoading();
                    collectIndex = index;
                    Video.collectVideo(memberId,videoId);
                }
            }
        };
        //获取网络数据成功的后调用该方法
        $scope.$on('Video.collectVideoUpdated',function(){
            collectVideoing = false;
            Loading.hideLoading();
            var result = Video.getCollectJson();
            $scope.videoList[collectIndex].vcStatus = result.vcStatus;
            Toast.showMessage(result.vcMsg);
        });
    })





